package com.zqx.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqx.domain.Dorm;
import com.zqx.service.DormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("dorm")
public class DormController {
    @Autowired
    private DormService dormService;

    @RequestMapping("listDorm")
    public String listDorm(@RequestParam(value = "pageNum", defaultValue = "1", required = false)Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "4", required = false)Integer pageSize, Dorm dorm, Model model){
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageNum = 1;
        }
        if (pageNum < 0 || pageNum.equals("") || pageNum == null) {
            pageSize = 4;
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        //条件构造器：MP用来查询一些特殊的字段
        //模糊查询
        QueryWrapper<Dorm> qw = new QueryWrapper<>();
        if (dorm.getBuild() != null) {
            qw.like("build", dorm.getBuild());
        }
        
        List<Dorm> list = dormService.list(qw);

        for (Dorm dorm1 : list) {
            if (dorm1.getSetting() >= dorm1.getPeople()){
                dorm1.setStatus(1);
                dormService.updateById(dorm1);
            }else {
                dorm1.setStatus(0);
                dormService.updateById(dorm1);
            }

        }
        PageInfo<Dorm> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);
        return "admin-dorm-list";
    }
    //添加宿舍信息
    @RequestMapping("saveDorm")
    public String saveDorm(Dorm dorm){
        boolean save = dormService.save(dorm);
        return "redirect:/dorm/listDorm";
    }
    //宿舍信息数据回显
    @RequestMapping("preUpdateDorm/{id}")
    public String preUpdateDorm(@PathVariable Integer id, Model model){
        Dorm byId = dormService.getById(id);
        model.addAttribute("dorm", byId);
        return "admin-dorm-update";
    }
    @RequestMapping("updateDorm")
    public String updateDorm(Dorm dorm){
        boolean b = dormService.updateById(dorm);
        return "redirect:/dorm/listDorm";
    }
    //删除宿舍信息
    @RequestMapping("delDorm/{id}")
    public String delStudent(@PathVariable Integer id){
        dormService.removeById(id);
        return "redirect:/dorm/listDorm";
    }
    //批量删除宿舍信息
    @ResponseBody
    @RequestMapping("batchDeleteDorm")
    public String batchDeleteStudent(String idList){
        String[] split = StrUtil.split(idList, ",");
        List<Integer> list = new ArrayList<>();
        for (String s:split
        ) {
            if (!s.isEmpty()){
                int i = Integer.parseInt(s);
                list.add(i);
            }
        }
        boolean b = dormService.removeByIds(list);
        if (b){
            return "OK";
        }else {
            return "error";
        }
    }
}
